// index.js
import {
  getProductListAPI
} from '../../api/product'
import requestUtil from '../../utils/requestUtil';

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const swiperList = [
  `https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/blog-img/202404171115702.jpeg`,
  `https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/blog-img/202404171110233.jpeg`,
  `https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/blog-img/202404171111662.png`,
  `https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/blog-img/202404171114535.png`,
  `https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/blog-img/202404171116194.jpeg`,
];

Page({
    data: {

    },
    onLoad: function (options) {
      // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
      var scene = decodeURIComponent(options.scene)
    },
  }),

  Component({
    data: {
      current: 0,
      autoplay: false,
      duration: 500,
      interval: 5000,
      swiperList,
      productList: [],
      searchValue: ""
    },

    methods: {
      onLoad: function (options) {
        this.getProductList()
      },
      onSearchChange(e) {
        this.setData({
          searchValue: e.detail,
        });
      },
      onSearch() {
        wx.navigateTo({
          url: `/pages/search/search?searchValue=${this.data.searchValue}`,
        });
        console.log('搜索' + this.data.searchValue);
      },
      onSearchClick() {
        wx.navigateTo({
          url: `/pages/search/search?searchValue=${this.data.searchValue}`,
        });
        console.log('搜索' + this.data.searchValue);
      },
      async getProductList() {
        const {
          code,
          data,
          result
        } = await getProductListAPI();
        this.setData({
          productList: result,
        })
      },
      jumpDetail(event) {
        const id = event.currentTarget.id;
        // switchTab tabBar页面跳转，navigateTo 非 tabBar页面跳转
        wx.navigateTo({
          url: `/pages/detail/detail?id=${id}`,
        });
      },
      scanCodeEvent: function () {
        var that = this;
        wx.scanCode({
          onlyFromCamera: true, // 只允许从相机扫码
          success(res) {
            console.log("扫码成功：" + JSON.stringify(res))

            // 扫码成功后  在此处理接下来的逻辑
            that.setData({
              scanCode: res.result
            })
          }
        })
      },
      touchend(event) {
        const id = event.currentTarget.id;
        wx.navigateTo({
          url: `/pages/detail/detail?id=${id}`,
        });
      },
      onTap(e) {
        const {
          index
        } = e.detail;
        console.log(index);
      },
      onChange(e) {
        const {
          current,
          source
        } = e.detail;

        console.log(current, source);
      },
      onImageLoad(e) {},
    },
  });